home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 68K / Lib / test / img / NOTYET / hexdump.py next >
Text File  |  1996-05-20  |  967b  |  57 lines

  1. """
  2. Make a hex dump of a file
  3. """
  4.  
  5. import sys
  6. sys.stdout = sys.stderr
  7.  
  8. import string
  9. import macfs
  10.  
  11. def main():
  12.     
  13.     # Ask for input if not specified in sys.argv[1:]
  14.     
  15.     if not sys.argv[1:]:
  16.         srcfss, ok = macfs.PromptGetFile('Input file to dump:')
  17.         if not ok:
  18.             return
  19.         filename = srcfss.as_pathname()
  20.         process(filename)
  21.     else:
  22.         
  23.         # Loop over all files to be processed
  24.         for filename in sys.argv[1:]:
  25.             process(filename)
  26.     sys.exit(1)
  27.     
  28. def process(filename):
  29.     print '------', filename
  30.     f = open(filename)
  31.     addr = -16
  32.     prevd = None
  33.     didskip = 0
  34.     while 1:
  35.         d = f.read(16)
  36.         if not d:
  37.             return
  38.         addr = addr + 16
  39.         if d == prevd:
  40.             if not didskip:
  41.                 print '*'
  42.             didskip = 1
  43.             continue
  44.         
  45.         outhex = ''
  46.         outstr = ''
  47.         for ch in d:
  48.             outhex = outhex + ('%02.2x '%ord(ch))
  49.             if ord(ch) < 32 or ord(ch) >= 0177:
  50.                 outstr = outstr + '.'
  51.             else:
  52.                 outstr = outstr + ch
  53.         print '%06.6x %-48.48s %s'%(addr, outhex, outstr)
  54.  
  55. if __name__ == '__main__':
  56.     main()
  57.